package com.admin.config;


import com.admin.annotations.EnableEureka;
import com.admin.annotations.EnableNacos;
import com.admin.annotations.EnableZookeeper;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class ConfigProcessorRegistry {
    // 定义一个映射，用于存储注解类与对应的配置处理器类
    private static final Map<Class<?>, Class<? extends ConfigProcessor>> registry = new HashMap<>();

    // 在静态初始化块中，将注解类与对应的处理器类注册到映射中
    static {
        registry.put(EnableNacos.class, NacosConfigProcessor.class);
        registry.put(EnableZookeeper.class, ZookeeperConfigProcessor.class);
        registry.put(EnableEureka.class, EurekaConfigProcessor.class);
        // 未来添加新的处理器时，只需在这里注册新的映射关系
    }

    // 根据注解类获取对应的配置处理器实例
    public static ConfigProcessor getProcessor(Class<?> annotationClass) {
        Class<? extends ConfigProcessor> processorClass = registry.get(annotationClass);
        if (processorClass != null) {
            try {
                // 通过反射创建处理器实例
                return processorClass.newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                // 创建实例失败时抛出运行时异常
                throw new RuntimeException("Unable to instantiate config processor", e);
            }
        }
        return null;
    }

    // 获取所有已注册的注解类
    public static Set<Class<?>> getRegisteredAnnotations() {
        return registry.keySet();
    }
}